package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.TicketDao;
import com.ChenYaRou.www.dao.impl.TicketDaoImpl;
import com.ChenYaRou.www.po.Ticket;
import com.ChenYaRou.www.po.TicketShop;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JdbcUtil;
import com.ChenYaRou.www.utils.JsonBean;
import com.ChenYaRou.www.utils.UpdateOrInsertOrSelectMoreAllBean;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.*;

/**
 * @ClassName GetTicketServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 11:38
 * @Version 1.0
 **/
public class GetTicketServletController {
    /**
     * 该为用户点击了店铺页面的领券功能后的 展示店铺的所有抵用券
     * @param shopId
     * @return
     */
    public String getTicket(Integer shopId){
        if(shopId!=null){
            TicketShop ticketShop=new TicketShop();
            ticketShop.setShopId(shopId);
            List<TicketShop> ticketShopList = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(ticketShop);
            if(ticketShopList!=null && !ticketShopList.isEmpty()){
                return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,ticketShopList);
            }
            return JsonBean.operateList(JsonBeanConstant.NULL,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 用户点击了获取抵用券 对用户的ticket进行insert 对店铺的ticket进行number--
     * @param shopId
     * @param discount
     * @param userId
     * @return
     */
    public String getTicket(Integer shopId,Float discount,Integer userId){
        if(shopId!=null  && !discount.isNaN() && userId!=null){
            TicketShop ticketShop=new TicketShop();
            ticketShop.setShopId(shopId);
            ticketShop.setDiscount(discount);
            //因为shopName和discount唯一 所以找出来的集合也只有唯一值ticketShop
            List<TicketShop> ticketShops = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(ticketShop);
            if(ticketShops!=null && !ticketShops.isEmpty()){
                TicketShop ticketShop1 = new TicketShop();
                ticketShop1.setNumber(ticketShops.get(0).getNumber()-1);
                //更新店铺对应折扣抵用券的ticketNumber -1
                Ticket ticket=new Ticket(userId,shopId,UN_USE, LocalDateTime.now().plusMonths(1L),discount);
                try {
                    String sqlOfTicketShop = GenerateSql.getInstance().update(ticketShop1,ticketShops.get(0));
                    String sqlOfUser = GenerateSql.getInstance().insert(ticket);
                    int result = JdbcUtil.getInstance().insertOrUpdateMore(sqlOfTicketShop, sqlOfUser);
                    if(result>0){
                        return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,null);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 用户在买单的时候判断是否有抵用券,有则供用户选择是否使用抵用券
     * @param ticket
     * @return
     */
    public String userHasTheShopTicket(Ticket ticket){
        if(ticket!=null){
            List<Ticket> ticketList = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(ticket);
            return JsonBean.operateList(JsonBeanConstant.NULL,ticketList);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    public String shopInsertTicket(TicketShop ticketShop){
        return UpdateOrInsertOrSelectMoreAllBean.getInstance().insert(ticketShop);
    }

    /**
     * 商家发放的是已拥有基数的抵用券,此刻ticketShop必须为最新状态的number,而不是增加值多少
     * @param ticketShop
     * @return
     */
    public String shopUpdateTicket(TicketShop ticketShop){
        TicketShop oldTicketShop=new TicketShop();
        oldTicketShop.setDiscount(ticketShop.getDiscount());
        oldTicketShop.setShopId(ticketShop.getShopId());
        System.out.println(ticketShop);
        System.out.println(oldTicketShop);
        return UpdateOrInsertOrSelectMoreAllBean.getInstance().update(ticketShop,oldTicketShop);
    }

    /**
     * 登录 时 更新抵用券状态
     * @return
     */
    public String updateTicketState(){
        try {
            int update=0;
            TicketDao ticketDao=new TicketDaoImpl();
            List<Ticket> ticketList = ticketDao.selectAll();
            for (Ticket ticket : ticketList) {
                int dead=ticket.getDeadline().compareTo(LocalDateTime.now());
                if(0>dead){
                    Ticket ticketNew=new Ticket();
                    ticketNew.setState(PAST);
                    update += ticketDao.update(ticketNew, ticket);
                }
            }
            if (update>0){
                return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,null);
            }
        } catch (SQLException | NoSuchFieldException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
    }
}
